home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / s_adalib.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  10KB  |  382 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  */
  8.  
  9. /* liblist.c: translation of code generator read.stl*/
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "config.h"
  15. #include "segment.h"
  16. #include "slot.h"
  17. #include "ifile.h"
  18. #include "miscp.h"
  19. #include "libfp.h"
  20.  
  21. static void load_library();
  22. static void get_local_ref_maps(IFILE *, int);
  23. static long get_cde_slots(IFILE *, Axq);
  24. static void get_slot(IFILE *, char *);
  25. static char *convert_date(char *);
  26. static char *unit_name_name(char *);
  27. static int is_subunit(char *);
  28. static char *formatted_name(char *);
  29. static char *formatted_stub(char *);
  30.  
  31. IFILE *LIBFILE;
  32.  
  33. #ifdef IBM_PC
  34. /* on the PC, a chdir must be undone before program completes.
  35.  * changed_dir is set when directory changed.
  36.  */
  37. #endif
  38.  
  39. void main(int argc, char **argv)
  40. {
  41.         int  c;
  42.         int  lib_opt = FALSE;
  43.         int  errflg = 0;
  44.  
  45.         extern int  optind;
  46.         extern char *optarg;
  47.     char *libname,*fname;
  48.     char *t_name;
  49.  
  50. #ifdef IBM_PC
  51.     fprintf(stderr, "NYU Ada/Ed Librarian Version 1.11.1\n");
  52.     fprintf(stderr, "Copyright (C) 1985-1992 by New York University.\n");
  53. #endif
  54.  
  55. #ifndef IBM_PC
  56.         while((c = getopt(argc,argv,"l:"))!=EOF) {
  57. #else
  58.         while((c = getopt(argc,argv,"L:l:"))!=EOF) {
  59.                 if (isupper(c)) c = tolower(c);
  60. #endif
  61.                 switch(c) {
  62.                 case 'l': /* specify library name */
  63.                         lib_opt = TRUE;
  64.                         libname = strjoin(optarg,"");
  65.                         break;
  66.                 case '?':
  67.                         errflg++;
  68.                 }        
  69.         }
  70.  
  71.     fname = (char *)0;
  72.     if (optind < argc) fname = argv[optind];
  73.         if (!lib_opt && fname == (char *)0) {
  74.         fname = getenv("ADALIB");
  75.         if (fname!= (char *)0) {
  76. #ifdef IBM_PC
  77.             fprintf(stderr, "L");
  78. #else
  79.             fprintf(stderr, "l");
  80. #endif
  81.             fprintf(stderr,"ibrary defined by ADALIB: %s\n", fname);
  82.         }
  83.     }
  84.     if ((!lib_opt && fname == (char *)0) || errflg) {
  85.         fprintf(stderr, "Usage: adalib [-l library]\n");
  86.         exit(1);
  87.     }
  88.         if (!lib_opt) {
  89.            libname = emalloc(strlen(fname) + 1);
  90.            strcpy(libname, fname);
  91.         }
  92.  
  93.     t_name = libset(libname);
  94.     LIBFILE = ifopen(LIBFILENAME, "", "r", 0);
  95.  
  96.     load_library();
  97.     exit(0);
  98. }
  99.  
  100. static void load_library()                                /*;load_library*/
  101. {
  102.     /*
  103.      * retrieve information from LIBFILE
  104.      * Called only if lib_option and not newlib.
  105.      */
  106.  
  107.     int        i, j, n, m, unumber, nodes, symbols;
  108.     int        comp_status, unit_count, cur_level;
  109.     char    *comp_date, *status_str, *uname, *aisname, *tmp_str;
  110.     char    *main_string;
  111.     int        is_main, empty_unit_slots, parent;
  112.     int        ignore;
  113.  
  114.  
  115.     unit_count = getnum(LIBFILE, "lib-unit-count");
  116.     n = getnum(LIBFILE, "lib-n");
  117.     empty_unit_slots = getnum(LIBFILE, "lib-empty-slots");
  118.     tmp_str = getstr(LIBFILE, "lib-tmp-str");
  119.     for (i = 1; i <= unit_count; i++) {
  120.         uname = getstr(LIBFILE, "lib-unit-name");
  121.         unumber = getnum(LIBFILE, "lib-unit-number");
  122.         aisname = getstr(LIBFILE, "lib-ais-name");
  123.         comp_date = getstr(LIBFILE, "unit-date");
  124.         symbols = getnum(LIBFILE, "lib-symbols");
  125.         nodes = getnum(LIBFILE, "lib-nodes");
  126.         is_main = getnum(LIBFILE, "lib-is-main");
  127.         if (is_main) {
  128.             if (streq(unit_name_type(uname), "ma"))
  129.                 main_string = "(Interface)";
  130.             else
  131.                 main_string = "  (Main)   ";
  132.         }
  133.         else {
  134.             main_string = "";
  135.         }
  136.         comp_status = getnum(LIBFILE, "lib-status");
  137.         status_str = (comp_status) ? "active  " : "obsolete";
  138.         printf("%8s %11s %-15s %s\n",
  139.             status_str, main_string, convert_date(comp_date),
  140.             formatted_name(uname));
  141.     }
  142.     printf("\n");
  143.     n = getnum(LIBFILE, "lib-n");
  144.     if (n) {
  145.         printf("stubs \n\n");
  146.         for (i = 1; i <= n; i++) {
  147.             uname = getstr(LIBFILE, "lib-unit-name");
  148.             aisname = getstr(LIBFILE, "lib-ais-name");
  149.             parent = getnum(LIBFILE, "lib-parent");
  150.             cur_level = getnum(LIBFILE, "lib-cur-level");
  151.             m = getnum(LIBFILE, "stub-file-size");
  152.             for (j = 1; j <= m; j++)
  153.                 ignore = getnum(LIBFILE, "stub-file");
  154.             printf("%s\n", formatted_stub(uname));
  155.         }
  156.         printf("\n");
  157.     }
  158.     ifclose(LIBFILE);
  159.     return;
  160. #ifdef TBSL
  161.     n = getnum(LIBFILE, "precedes-map-size");
  162.     printf("precedes map\n");
  163.     for (i = 1; i <= n; i += 2) {
  164.         dom = getnum(LIBFILE, "precedes-map-dom");
  165.         m = getnum(LIBFILE, "precedes-map-nelt");
  166.         printf("  %4d:", dom);
  167.         for (j = 1; j <= m; j++) {
  168.             range = getnum(LIBFILE, "precedes-map-ent");
  169.             printf(" %4d", range);
  170.         }
  171.         printf("\n");
  172.     }
  173.     n = getnum(LIBFILE, "compilation_table_size");
  174.     if (n) {
  175.         printf("\ncompilation table\n");
  176.         for (i = 1; i <= n; i++) {
  177.             unum = (int) getnum(LIBFILE, "compilation-table-ent");
  178.             printf("  %d\n", unum);
  179.         }
  180.         printf("\n");
  181.     }
  182.     /* late_instances */
  183.     n = getnum(LIBFILE, "late-instances-size");
  184.     if (n) {
  185.         printf("late instances\n");
  186.         for (i = 1; i <= n; i++) {
  187.             str = (char *) getstr(LIBFILE, "late-instances-str");
  188.             printf("  %s\n", str);
  189.         }
  190.     }
  191.     /* current code segment */
  192.     n = getnum(LIBFILE, "unit-size");
  193.     printf("\ncurrent code segments\n");
  194.     printf("  unit cs\n");
  195.     for (i = 1; i <= n; i++) {
  196.         cs = getnum(LIBFILE, "current-code-segment");
  197.         if (cs) printf("   %d: %d\n", i, cs);
  198.     }
  199.     /* local reference maps */
  200.     n = getnum(LIBFILE, "unit-size");
  201.     get_local_ref_maps(LIBFILE, n);
  202.     cde_pos = get_cde_slots(LIBFILE, axq);
  203.  
  204.  
  205.     /* could free axq_data_slots, etc., but keep for now */
  206.     /* read out LIB_STUB map (always empty for now) */
  207.     ifclose(LIBFILE);
  208.     return;
  209. #endif
  210. }
  211.  
  212. static void get_local_ref_maps(IFILE *ifile, int units)    /*;get_local_ref_map*/
  213. {
  214.     int        unit, defined, i, off, n;
  215.     int        sym_seq, sym_unit;
  216.  
  217.     printf("\nlocal reference maps\n");
  218.     for (unit = 1; unit <= units; unit++) {
  219.         /* ignore empty ref maps (predef units) and obselete units */
  220.         defined = getnum(ifile, "local-ref-map-defined");
  221.         if (!defined) continue;
  222.         printf("%d: ", unit);
  223.         n = getnum(ifile, "local-ref-map-size");
  224.         n = n/2;
  225.         for (i = 1; i <= n; i++) {
  226.             sym_seq = getnum(ifile, "local-ref-map-sym-seq");
  227.             sym_unit = getnum(ifile, "local-ref-map-sym-unit");
  228.             off = getnum(ifile, "local-ref-map-off");
  229.             /* if all three values are zero ignore this entry. It is a fake
  230.              * entry created by put_local_ref_map. see comment there.
  231.              */
  232.             if (sym_seq == 0 && sym_unit == 0 && off == 0) continue;
  233.             printf("%d %d %d ", sym_seq, sym_unit, off);
  234.         }
  235.         printf("\n");
  236.     }
  237.     printf("\n");
  238. }
  239.  
  240. static long get_cde_slots(IFILE *file, Axq axq)                /*;get_cde_slots*/
  241. {
  242.     long    dpos;
  243.     int        n_code, n_data, n_exception;
  244.  
  245.  
  246.     dpos = file->fh_slots;
  247.     /* position to start of slot info */
  248.     ifseek(file, "get-cde-slots-start", dpos, 0);
  249.     n_code = getnum(file, "n-code");
  250.     n_data = getnum(file, "n-data");
  251.     n_exception = getnum(file, "n-exception");
  252.     get_slot(file, "code");
  253.     get_slot(file, "data");
  254.     get_slot(file, "exceptions");
  255.     return dpos; /* return offset of start of slot info */
  256. }
  257.  
  258. static void get_slot(IFILE *file, char *name)                    /*;get_slot*/
  259. {
  260.     /* This procedure reads in the SLOTS information. 
  261.      * Entries are Slots structures. nmax is guess at needed dimension,
  262.      * dim is set to dimension actually found.
  263.      */
  264.  
  265.     int i, n, exists;
  266.     int    slot_seq, slot_unit, slot_number;
  267.     char *slot_name;
  268.  
  269.     n = getnum(file, "slot-entries");
  270.     printf("%s slots \n", name);
  271.     printf("  num seq unit  name\n");
  272.  
  273.     for (i = 1; i <= n; i++) {
  274.         exists = getnum(LIBFILE, "slot-exists");
  275.         if (exists) {
  276.             slot_seq = getnum(LIBFILE, "slot-seq");
  277.             slot_unit = getnum(LIBFILE, "slot-unit");
  278.             slot_number = getnum(LIBFILE, "slot-number");
  279.             slot_name = getstr(LIBFILE, "slot_name");
  280.             printf("  %3d %3d %4d  %s\n",
  281.                 slot_number, slot_seq, slot_unit, slot_name);
  282.         }
  283.     }
  284.     printf("\n");
  285. }
  286.  
  287. static char *convert_date(char *comp_date)                    /*;convert_date*/
  288. {
  289.     static char new_date[15];
  290.     int    i;
  291.  
  292.     if (comp_date == (char *)0)
  293.         return(" ");
  294.     comp_date++;
  295.     comp_date++;
  296.     for (i = 6; i < 8; i++)
  297.         new_date[i] = *comp_date++;
  298.     comp_date++;
  299.     for (i = 0; i < 2; i++)
  300.         new_date[i] = *comp_date++;
  301.     new_date[2] = '/';
  302.     comp_date++;
  303.     for (i = 3; i < 5; i++)
  304.         new_date[i] = *comp_date++;
  305.     new_date[5] = '/';
  306.     new_date[8] = ' ';
  307.     comp_date++;
  308.     for (i = 9; i < 14; i++)
  309.         new_date[i] = *comp_date++;
  310.     new_date[11] = ':';
  311.  
  312.     new_date[14] = '\0';
  313.     return new_date;
  314. }
  315.  
  316. static char *unit_name_name(char *u)                        /*;unit_name_name*/
  317. {
  318.     int    n;
  319.     char    *s1, *s2;
  320.  
  321.     n = strlen(u);
  322.     if (n <= 2)
  323.         return (char *)0;
  324.  
  325.     s1 = u+2;                 /* point to start of name*/
  326.     s2 = strchr(s1, '.');     /* look for dot after first name */
  327.     if (s2 == (char *)0)     /* if no dot take rest of string */
  328.         s2 = u + n;         /* find end */
  329.     n = s2 - s1;
  330.     s2 = emalloc((unsigned) n+1);
  331.     strncpy(s2, s1, n);
  332.     s2[n] = '\0'; /* terminate result */
  333.     return (s2);
  334. }
  335.  
  336. static int is_subunit(char *u)                                    /*;is_subunit*/
  337. {
  338.     /* In C, IS_SUBUNIT is procedure is_subunit():
  339.      *    IS_SUBUNIT(na);           (#na > 2)                          endm;
  340.      */
  341.  
  342.     int    n;
  343.     char    *s1, *s2;
  344.  
  345.     if (u == (char *)0)
  346.         chaos("is_subunit: null pointer");
  347.     n = strlen(u);
  348.     if (n <= 2)
  349.         return FALSE;
  350.     s1 = u + 2; /* point to start of name*/
  351.     s2 = strchr(s1, '.'); /* look for dot after first name */
  352.     if (s2 == (char *)0) /* if no dot take rest of string */
  353.         return FALSE;
  354.     return TRUE; /* if subunit*/
  355. }
  356.  
  357. static char *formatted_name(char *unit)                    /*;formatted_name*/
  358. {
  359.     char *kind, *unit_kind;
  360.  
  361.     kind = unit_name_type(unit);
  362.     if (is_subunit(unit))        unit_kind = "proper body ";
  363.     else if (streq(kind, "sp"))  unit_kind = "package spec ";
  364.     else if (streq(kind, "bo"))  unit_kind = "package body ";
  365.     else if (streq(kind, "ss"))  unit_kind = "subprogram spec ";
  366.     else if (streq(kind, "su"))  unit_kind = "subprogram ";
  367.     else if (streq(kind, "ma"))  unit_kind = "binding unit ";
  368.     else unit_kind = "unit ";
  369.     return strjoin(unit_kind, unit_name_name(unit));
  370. }
  371.  
  372. static char *formatted_stub(char *unit)                        /*;formatted_stub*/
  373. {
  374.     char *kind, *unit_kind;
  375.  
  376.     kind = unit_name_type(unit);
  377.     if (streq(kind, "bo"))  unit_kind = "package (task) stub ";
  378.     else if (streq(kind, "su"))  unit_kind = "subprogram stub ";
  379.     else unit_kind = "stub ";
  380.     return strjoin(unit_kind, unit_name_name(unit));
  381. }
  382.